package com.example.w.web.controller;

import com.example.w.common.dto.OrderAddDTO;
import com.example.w.common.pojo.Order;
import com.example.w.common.util.Result;
import com.example.w.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
@Api(tags = "订单管理接口")
public class OrderController {

    @Autowired
    OrderService orderService;

    /**
     * 根据桌号和支付状态查询订单
     */
    @GetMapping("/getOrderByTableId")
    @ApiOperation("根据桌号和支付状态查询订单，订单状态，0表示未支付，1表示已支付")
    //@RequiresRoles(value = {"cashier", "admin"}, logical = Logical.OR)
    public Result getOrderByTableId(Integer tableId, Integer paystatus) {
        List<Order> target = orderService.getOrderByTableIdAndPayStatus(tableId, paystatus);
        return new Result(true, "查找成功", target);
    }

    /**
     * 添加订单，返回总金额
     */
    @PostMapping("/addOrder")
    @ApiOperation("下单")
    //@RequiresRoles(value = {"cashier", "admin"}, logical = Logical.OR)
    public Result addOrder(@RequestBody OrderAddDTO orderAddDTO){
        Order order = orderAddDTO.convertToOrder();
        orderService.addOrder(order);
        return new Result(true,"添加成功",order.getTotal());
    }

    /**
     * 换台
     */
    @GetMapping("/changeTable")
    @ApiOperation("换台")
    //@RequiresRoles(value = {"cashier", "admin"}, logical = Logical.OR)
    public Result changeTable(@RequestParam long orderId, @RequestParam int tableId){
        orderService.changeTable(orderId, tableId);
        return new Result(true,"换台成功");
    }

    /**
     * 加菜，返回总金额
     */
    @PostMapping("additionalMenuItem")
    @ApiOperation("加菜")
    //@RequiresRoles(value = {"cashier", "admin"}, logical = Logical.OR)
    public Result additionalMenuItem(@RequestBody OrderAddDTO addedOrder) {
        Order order = addedOrder.convertToOrder();
        orderService.additionalMenuItem(order);
        return new Result(true, "加菜成功", order.getTotal());
    }

    /**
     *根据订单号查询订单
     */
    @GetMapping("/getOrderByOrderId")
    @ApiOperation("根据订单号查询订单")
    public Result getOrderByOrderId(@RequestParam Long id) {
        Order target = orderService.gerOrderByOrderId(id);
        return new Result(true, "查找成功", target);
    }

    /**
     * 根据支付状态查询订单
     */
    @GetMapping("/getOrderByPaystatus")
    @ApiOperation("根据支付状态查询订单")
    public Result getCurrentOrder(@RequestParam Integer tableId) {
        return new Result(true, "查找成功", orderService.getCurrentOrder(tableId));
    }

}
